﻿
$(document).ready(function () {
    //    $("#txt_frdate").datepicker({ dateFormat: 'dd/mm/yy' });
    //    $("#txt_todate").datepicker({ dateFormat: 'dd/mm/yy' });
    //$("#plistdatefrom").datepicker({ dateFormat: 'dd/mm/yy' });
    //$("#plistdateto").datepicker({ dateFormat: 'dd/mm/yy' });
});

function checkNuber(num) {
    if (num != "") {
        if (isNaN(num) == true) {
            alert("Yêu cầu nhập số!");
            return false;
        }
        return true;
    }
    return true;
}

function CheckValue() {

    var textBoxes = $("input[type='text']");
    for (var i = 0; i < textBoxes.length; i++) {
        value = textBoxes[i].value;
        if (value != '' && value != null) {
            if (!IsNumeric(value)) {
                alert("Yêu cầu nhập số!");
                textBoxes[i].focus();
                return false;
            }
        } else {
            textBoxes[i].value = 0;
        }
    }
    return true;
}


function IsNumeric(sText) {
    var ValidChars = "0123456789";
    var IsNumber = true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}

function ClientStatus() {
    alert("Chuyên mục này đang xây dựng!");
    return;
}

function ClientShowAddnewInterCoopForm() {
    Url = Portal.SODC.IDIS.ListInterCoop.GetShowAddNewsForm().value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    retValue = window.open(Url, "_blank");
}

function ClientShowEditCrimeForm(RecordId) {
    Url = Portal.SODC.IDIS.ListInterCoop.GetShowUpdateForm(RecordId).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    retValue = window.open(Url, "_blank");
}


function ClientShowDetailInterCoop(RecordId) {
    Url = Portal.SODC.IDIS.ListInterCoop.GetShowDetailForm(RecordId).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    retValue = window.open(Url, "_blank");
}


function ClientUpdateInterCoop(NumUnit, RecordId) {    
    frdate = document.getElementById("drpDaytxt_frdate").options[document.getElementById("drpDaytxt_frdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_frdate").options[document.getElementById("drpMonthtxt_frdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_frdate").options[document.getElementById("drpYeartxt_frdate").selectedIndex].value;
    if (frdate == "//") {
        alert("Yêu cầu nhập ngày tháng!!!");
        return;
    } else {
        if (!CheckValue()) {
            return;
        } else {
            var ArrayUnit = Array();
            for (var iIndex = 0; iIndex < NumUnit; iIndex++) {
                value = document.getElementById("txt" + iIndex).value;
                ArrayUnit[iIndex] = document.getElementById("txt" + iIndex).value;
            }
            RetValue = Portal.SODC.IDIS.EditInterCoop.ServerSideSaveInterCoop(RecordId, frdate, ArrayUnit).value;
            if (RetValue.indexOf("ERROR") != -1) {
                alert(RetValue.replace("ERROR: ", ""));
                return;
            } else {
                alert(RetValue);
            }
        }
    }
}

function ClientDoSaveInterCoop(NumUnit) {
    
    frdate = document.getElementById("drpDaytxt_frdate").options[document.getElementById("drpDaytxt_frdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_frdate").options[document.getElementById("drpMonthtxt_frdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_frdate").options[document.getElementById("drpYeartxt_frdate").selectedIndex].value;
    if (frdate == "//") {
        alert("Yêu cầu nhập ngày tháng!!!");
        return;
    } else {
        if (!CheckValue()) {
            return;
        } else {
            var ArrayUnit = Array();
            for (var iIndex = 0; iIndex < NumUnit; iIndex++) {
                value = document.getElementById("txt" + iIndex).value;
                ArrayUnit[iIndex] = document.getElementById("txt" + iIndex).value;
            }
            RetValue = Portal.SODC.IDIS.AddNewInterCoop.ServerSideAddNewInterCoop(frdate, ArrayUnit).value;
            if (RetValue.indexOf("ERROR") != -1) {
                alert(RetValue.replace("ERROR: ", ""));
                return;
            } else {
                alert(RetValue);
                ResetAllFieldInterCoop(NumUnit);
            }
        }
    }
}

function isValidDate(sText) {
    var reDate = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
    return reDate.test(sText);
}


function ResetAllFieldInterCoop(NumUnit) {
    var textBoxes = $("input[type='text']");
    for (var i = 0; i < textBoxes.length; i++) {
        textBoxes[i].value = "";
    }
}



function ClientDoSimpleSearchInterCoop() {
    var frDate = document.getElementById("drpDaytxt_frdate").options[document.getElementById("drpDaytxt_frdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_frdate").options[document.getElementById("drpMonthtxt_frdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_frdate").options[document.getElementById("drpYeartxt_frdate").selectedIndex].value;
    var toDate = document.getElementById("drpDaytxt_todate").options[document.getElementById("drpDaytxt_todate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_todate").options[document.getElementById("drpMonthtxt_todate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_todate").options[document.getElementById("drpYeartxt_todate").selectedIndex].value;

    elem = document.getElementById("Notkey");

    if (frDate == "//") {
        frDate = null;
    }
    if (toDate == "//") {
        toDate = null;
    }
    result = document.getElementById("ContentSearchResult");
    ps = null;
    var retVal = Portal.SODC.IDIS.ListInterCoop.GetResult('app', frDate, toDate, 0, "JumpPageSearchResultInterCoop", ps).value;
    if (retVal.indexOf("ERROR") != -1) {
        alert(retVal.replace("ERROR: ", ""));
        return;
    }
    result.innerHTML = retVal;
    if (frDate == "//" || toDate == "//") {
        alert("Chưa xác định được thời gian tìm kiếm!");
        document.getElementById("divProcessingSearchResult").innerHTML = "";
    }
}

function JumpPageSearchResultInterCoop(wp, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    document.getElementById("divProcessingSearchResult").innerHTML = Portal.SODC.IDIS.ListInterCoop.GetProcessingText().value;
    setTimeout("RealJumpPageSearchResultInterCoop('','" + MaxItems + "', 20, 10, '" + CurrentPage + "','RealJumpPageSearchResultInterCoop')", 10);
}
function RealJumpPageSearchResultInterCoop(webpartid, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    var LocationId = null;
    PageIndex = CurrentPage;
    var frDate = document.getElementById("drpDaytxt_frdate").options[document.getElementById("drpDaytxt_frdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_frdate").options[document.getElementById("drpMonthtxt_frdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_frdate").options[document.getElementById("drpYeartxt_frdate").selectedIndex].value;
    var toDate = document.getElementById("drpDaytxt_todate").options[document.getElementById("drpDaytxt_todate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_todate").options[document.getElementById("drpMonthtxt_todate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_todate").options[document.getElementById("drpYeartxt_todate").selectedIndex].value;
    ps = null;
    retValue = Portal.SODC.IDIS.ListInterCoop.GetResult(webpartid, frDate, toDate, CurrentPage, JumpJavaScript, ps).value;
    if (retValue.indexOf("ERROR") != -1) {
        alert(retValue);
        return;
    }
    document.getElementById("divProcessingSearchResult").innerHTML = "";
    document.getElementById("ContentSearchResult").innerHTML = retValue;
}

// Delete InterCoop Info
function DeleteInterCoopInfo(RecordId) {
    Msg = Portal.SODC.IDIS.ListInterCoop.GetConfirmDeleteMsg().value;
    if (confirm(Msg) == false) return;
    retVal = Portal.SODC.IDIS.ListInterCoop.ServerSideDeleteInterCoop(RecordId).value;
    if (retVal.indexOf("ERROR") != -1) {
        alert(retVal);
        return;
    }
    alert(retVal);
    ClientDoSearchByLocation();
}

function ClientSideExportDataInterCoop() {

    var frDate = document.getElementById("drpDaytxt_frdate").options[document.getElementById("drpDaytxt_frdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_frdate").options[document.getElementById("drpMonthtxt_frdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_frdate").options[document.getElementById("drpYeartxt_frdate").selectedIndex].value;
    var toDate = document.getElementById("drpDaytxt_todate").options[document.getElementById("drpDaytxt_todate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_todate").options[document.getElementById("drpMonthtxt_todate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_todate").options[document.getElementById("drpYeartxt_todate").selectedIndex].value;
    elem = document.getElementById("Notkey");
    if (frDate == "//") {
        frDate = null;
    }
    if (toDate == "//") {
        toDate = null;
    }
    Ret = Portal.SODC.IDIS.ListInterCoop.ServerExportDataInterCoop(frDate, toDate).value;
    document.getElementById("divProcessingSearchResult").innerHTML = "Hệ thống đang xử lý";
    if (Ret.indexOf("ERROR") != -1) {
        alert(Ret.replace("ERROR: ", ""));
        document.getElementById("divProcessingSearchResult").innerHTML = "";
        return;
    } else {
        document.getElementById("divProcessingSearchResult").innerHTML = "";
        window.open(Ret, "_blank");
    }
    if (frDate == "//" || toDate == "//") {
        alert("Chưa xác định được thời gian tìm kiếm!");
        document.getElementById("divProcessingSearchResult").innerHTML = "";
        return;
    }
}
